package com.yourcompany.api.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;
import com.yourcompany.api.enums.LoginEventEnum;
import com.yourcompany.api.dto.UserLoginDTO;
import com.yourcompany.api.entity.User;

@Getter
public class LoginEvent extends ApplicationEvent {
    private final String username;
    private final Long userId;
    private final String loginType;
    private final String deviceId;
    private final String userAgent;
    private final Integer status;
    private final String msg;
    
    public static LoginEvent of(Object source, User user, UserLoginDTO loginDTO, 
            LoginEventEnum eventEnum) {
        return new LoginEvent(
            source,
            user != null ? user.getUsername() : loginDTO.getUsername(),
            user != null ? user.getId() : null,
            "account",
            loginDTO.getDeviceId(),
            loginDTO.getUserAgent(),
            eventEnum.getStatus(),
            eventEnum.getMessage()
        );
    }
    
    public LoginEvent(Object source, String username, Long userId, String loginType, 
            String deviceId, String userAgent, Integer status, String msg) {
        super(source);
        this.username = username;
        this.userId = userId;
        this.loginType = loginType;
        this.deviceId = deviceId;
        this.userAgent = userAgent;
        this.status = status;
        this.msg = msg;
    }

    // 添加一个静态内部构建器类
    public static class LoginEventBuilder {
        private Object source;
        private String username;
        private Long userId;
        private String loginType;
        private String deviceId;
        private String userAgent;
        private Integer status;
        private String msg;

        public LoginEventBuilder source(Object source) {
            this.source = source;
            return this;
        }

        public LoginEventBuilder username(String username) {
            this.username = username;
            return this;
        }

        public LoginEventBuilder userId(Long userId) {
            this.userId = userId;
            return this;
        }

        public LoginEventBuilder loginType(String loginType) {
            this.loginType = loginType;
            return this;
        }

        public LoginEventBuilder deviceId(String deviceId) {
            this.deviceId = deviceId;
            return this;
        }

        public LoginEventBuilder userAgent(String userAgent) {
            this.userAgent = userAgent;
            return this;
        }

        public LoginEventBuilder status(Integer status) {
            this.status = status;
            return this;
        }

        public LoginEventBuilder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public LoginEvent build() {
            return new LoginEvent(source, username, userId, loginType, deviceId, userAgent, status, msg);
        }
    }

    public static LoginEventBuilder builder() {
        return new LoginEventBuilder();
    }
} 